package com.sunyard.utils;

import com.google.common.base.Preconditions;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.Optional;

/**
 * @author ：xiaog.li
 * @date ： 2019/5/11 17:02
 * @description：工具类
 * @version: 1.0$
 */
public class CommentUtil {
    /**
     * 获取两点之间的距离
     *
     * @param *[lat1, lng1, lat2, lng2]
     * @return double
     * @author xiaogang
     * @date 2019/4/2 10:23
     */
    public static double getDistance(String lat1, String lng1, String lat2, String lng2) {
        double earthRadius = 6378137.0;
        /**
         * A经弧度
         */
        double radiansX1 = Math.toRadians(Double.parseDouble(lng1));
        /**
         *A纬弧度
         */
        double radiansY1 = Math.toRadians(Double.parseDouble(lat1));
        /**
         *B经弧度
         */
        double radiansX2 = Math.toRadians(Double.parseDouble(lng2));
        /**
         *B纬弧度
         */
        double radiansY2 = Math.toRadians(Double.parseDouble(lat2));
        double cos = Math.cos(radiansY1) * Math.cos(radiansY2) * Math.cos(radiansX1 - radiansX2)
                + Math.sin(radiansY1) * Math.sin(radiansY2);
        /**
         * 反余弦值
         */
        double acos = Math.acos(cos);
        return earthRadius * acos;
    }


    public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";

    /**
     * 将分为单位的转换为元 （除100）
     *
     * @author xiaog.li
     * @date 2019/7/10 17:41
     */
    public static String toYuan(String fen) {
        if (StringUtils.isEmpty(fen)) {
            return "0";
        }
        if (!fen.matches(CURRENCY_FEN_REGEX)) {
            throw new IllegalArgumentException("金额格式有误");
        }
        return BigDecimal.valueOf(Long.valueOf(fen)).divide(new BigDecimal(100)).setScale(2).toString();
    }

    /**
     * @param fen
     * @Author: pengfei.yuan
     * @Date: 2020-11-05 15:55
     * @Version: V1.0
     * @Desc:单位转换
     **/
    public static BigDecimal toYuan(BigDecimal fen) {
        return Optional.ofNullable(fen).map(val -> val.divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP))
                .orElse(BigDecimal.ZERO);
    }

    public static final String CURRENCY_YUAN_REGEX = "^\\d*\\.\\d{2}$";

    /**
     * 转换为分
     *
     * @param yuan
     * @return
     */
    public static String toFen(String yuan) {
        if (StringUtils.isEmpty(yuan)) {
            return "0";
        }

        BigDecimal bigDecimal = new BigDecimal(yuan);
        bigDecimal = bigDecimal.multiply(new BigDecimal(100));
        return String.valueOf(bigDecimal.longValue());
    }


    /**
     * @param yuan
     * @Author: pengfei.yuan
     * @Date: 2020-11-05 15:55
     * @Version: V1.0
     * @Desc:单位转换
     **/
    public static BigDecimal toFen(BigDecimal yuan) {
        return Optional.ofNullable(yuan).map(val -> val.multiply(new BigDecimal("100")))
                .orElse(BigDecimal.ZERO);
    }

    /**
     * 校验String字段是否为空
     *
     * @author xiaog.li
     * @date 2019/7/10 17:42
     */
    protected void verifyString(String value, String msg) {
        Preconditions.checkArgument(!StringUtils.isEmpty(value), msg);
    }

    /**
     * 数字类型处理，去掉整数前边的0
     *
     * @param fen 该参数必须是分
     * @return
     */
    public static String toFenHandler(String fen) {
        if (StringUtils.isEmpty(fen)) {
            return "0";
        }
        if (!fen.matches(CURRENCY_FEN_REGEX)) {
            throw new NumberFormatException("该金额不是数字类型字符");
        }
        return String.valueOf(Long.parseLong(fen));
    }
}
